package name.jiaosha.air.company.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.company.domain.ExpenseItem;
import name.jiaosha.air.company.service.ICompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;
/**
 * 费用项目
 * @author xu
 *
 */
@Controller
@RequestMapping("/admin/com/")
public class ExpenseItemController {

	@Autowired
	private ICompanyService companyService;

	@RequestMapping("/expenseItems")
	public ModelAndView getMyExpenseItems() throws Exception {

		PageInfo<ExpenseItem> pageInfo = null;

		if (!AppConstants.DEBUG) {
			List<ExpenseItem> list = new ArrayList<ExpenseItem>();
			for (int i = 1; i <= 20; i++) {

				ExpenseItem item = new ExpenseItem();
				item.setId(i);
				item.setName("打孔" + i);
				item.setExpense(new BigDecimal(8.05));
				list.add(item);
			}

		} else {
			
			pageInfo=companyService.getExpenseItemByComId(AppConstants.comId, 1, AppConstants.DEFAULT_SIZE);

		}

		ModelMap model = new ModelMap();
		
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/expenseItemAdd")
	public ModelAndView createExpenseItem(@RequestParam String name,
			@RequestParam String expense) {

		if (!AppConstants.DEBUG) {

		} else {

			ExpenseItem item = new ExpenseItem();
			item.setName(name.trim());
			item.setExpense(new BigDecimal(expense));
			companyService.createExpenseItem(item);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/expenseItemUpdate")
	public ModelAndView updateExpenseItem(@RequestParam int id,
			@RequestParam String name, @RequestParam String expense) {

		if (!AppConstants.DEBUG) {

		} else {

			ExpenseItem item = companyService.getExpenseItemById(id);
			item.setName(name.trim());
			item.setExpense(new BigDecimal(expense));
			companyService.updateExpenseItem(item);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

}
